package harlequin;

import java.io.IOException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class Login extends JFrame {

    private String userNameLog;
    private String passwordLog;
    
    private String userNameReg;
    private String passwordReg;
    private String cpassword;
    private String fullName;
    private Connection connector;
    private String query = "insert into admin(username, password, name"
                      + ") values(?,?,?)";
    
    
    public Login(Connection connector) {
        this.connector = connector;
        this.setLocationByPlatform(rootPaneCheckingEnabled);
        this.setTitle("HTG - Login");
        initComponents();
       
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tUsername = new javax.swing.JTextField();
        bLogin = new javax.swing.JButton();
        bAttendance = new javax.swing.JButton();
        tPassword = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Username:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 344, -1, -1));

        jLabel2.setText("Password:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 373, -1, -1));
        getContentPane().add(tUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 341, 228, -1));

        bLogin.setText("Login");
        bLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLoginActionPerformed(evt);
            }
        });
        getContentPane().add(bLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 410, 296, -1));

        bAttendance.setText("Attendance");
        bAttendance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAttendanceActionPerformed(evt);
            }
        });
        getContentPane().add(bAttendance, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 442, 296, -1));
        getContentPane().add(tPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 370, 228, -1));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/HTGBG320480.jpg"))); // NOI18N
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(3, -2, 320, 480));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void bAttendanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAttendanceActionPerformed
    new AttendanceDLSU(connector).setVisible(true);  
}//GEN-LAST:event_bAttendanceActionPerformed
public boolean UserNameTaken(String userNameReg) throws SQLException{
    PreparedStatement preparedStatement = connector.prepareStatement(query);
    String query = "select * from admin";
   ResultSet set = preparedStatement.executeQuery(query);
    
    
    while(set.next()){
        if(set.getString("username").equals(userNameReg)) 
            return true;
    }
    
    return false;
}

public boolean LoginChecker() throws SQLException{
    userNameLog = tUsername.getText();
    PreparedStatement preparedStatement = connector.prepareStatement(query);
     passwordLog = tPassword.getText();
    String query = "select * from admin";
   ResultSet set = preparedStatement.executeQuery(query);
    
    
    while(set.next()){
        if(set.getString("username").equals(userNameLog) && set.getString("password").equals(passwordLog))
            return true;
    }
    
    return false;
}

private void bLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLoginActionPerformed
            try {
            if(LoginChecker()){
               this.dispose();
               new HomePage(connector, userNameLog).setVisible(true);
            }
            else{
                JOptionPane.showMessageDialog(null, "Wrong Username or Password");
            }
               
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_bLoginActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAttendance;
    private javax.swing.JButton bLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField tPassword;
    private javax.swing.JTextField tUsername;
    // End of variables declaration//GEN-END:variables
}
